\subsection{Class Diagram}
\label{classDiagramAndroidSection}

The Android application needs to contact with the \emph{Find Friend Location} main server in order to access the Web service's 
public methods. This is going to be done by using the \emph{SOAP} message protocol in \emph{XML} over the \emph{HTTP} protocol. The Android 
application also needs to send request messages with the serialized parameteres and the web server is going to return the 
response messages with serialized values.

A main problem the android's \emph{SDK} has, is that does not provide classes to work with Web services. So, it needs to work manually 
with Web services by using \emph{SOAP}. But a library called \emph{kSOAP2} helps a lot with the implementation. This library is able to create, 
send, receive and parse soap messages. Another important point is that this library is able to serialize and deserialize 
objects. This is called mapping, so object classes are mapped. In order to make the classes serializable they all must implement 
the \emph{KVMSerializable} interface in order to be serializable by the \emph{KSoap2} library.

Another problem was the number of requests to consume the web service has to be low. The solution to reduce the amount of 
information that needed to be transmitted across the network is a cache system. So it is decided to use an adapter class 
to access the consumption of the web service and store a simple or small cache of some response methods. It is also taken into 
consideration to join notes if they are too close, so they will be displayed on the map like a group. The clustering algorithm 
is chosen so as to solve this issue.

\begin{figure}[ht]
\centering
\includegraphics[width=0.9\textwidth ]{./Android/Design/images/classDiagram.png} 
\caption{Class Diagram}
\label{classDiagramAndroid}
\end{figure}
\clearpage

As is shown in the designed class diagram \ref{classDiagramAndroid} for Android's application, five classes are used for the 
model, two for note clustering and one as an adapter to the web service.

The web server developer has made the model classes public, so it was decided to extend and use them in this application. 
More information about these classes are in the \emph{Web Service} chapter. All classes that are extended from the server 
start with the \emph{K} letter, like \emph{KAuth}, \emph{KUserInfo}, \emph{KPosition}, \emph{KNote} or \emph{KPhoto} and they 
implement \emph{KvmSerializable} so as to be serializable using the \emph{KSoap2} library for android.

Some classes like \emph{KPosition} or \emph{KPhoto} add some methods that can be useful when developing the application in 
Android, the most of them are related to the GPS positions or map drawing. The \emph{Group} class stores notes that have been 
added by the \emph{Groups} class which holds more than one groups. When a new note is added to \emph{Groups}, this class 
decides whether this note should be in a new group or added to an existing one, depending on the distance. One thing worth 
mentioning is that when a new note is added to a group, the groups position is calculated again based on all notes positions 
in the group.

Finally there is the \emph{ToServer} class, that contains all the static methods that the rest of the classes will call so as 
to access the Web server. This happens because the class works like an adapter to the main web service. It provides access to 
all web service methods that are used in the Android application and caches some of the responses for a specific time in order 
to avoid unnecessary calls through the network.

Classes that have position, like \emph{KUserInfo} and \emph{KNote}, have the \emph{getPosition} method overwritten so as 
to return a \emph{KPosition} object which includes the methods that \emph{Position} class does not. This is a simple casting 
because these classes will always have the \emph{KPosition} extension of the \emph{Position} class. The \emph{KPosition} class 
has also methods to calculate the distance between two of these objects, so as to get a \emph{GeoPoint}, used by the operating 
system, and methods to get the latitude and longitude as floats.

The \emph{Group} class has \emph{Note} objects and a position, which is deduced from the notes positions, in the 
\emph{updatePosition} method. It has a method that adds a note and a function that returns an iterator with the notes that 
contains. It also offers functions that are used when drawing the group, like \emph{getMessage}, \emph{getName}, 
\emph{hasPhoto} or \emph{isGroup}. This last one returns if there is more than one notes in the group. You can see the sequence diagram
that will specify this method in \emph{Figure \ref{secuenceDiagramClasifyNotes}}

\begin{figure}[ht]
  \centering
  \includegraphics[width=1.0\textwidth]{./Android/Design/images/clasifyNotes.png} 
  \caption{Creating note groups}
  \label{secuenceDiagramClasifyNotes}
\end{figure}


So as to create groups the \emph{Groups} class must be used. This class contains the list of \emph{Group} objects and the minimun 
distance they must have. The class has methods to add notes, which are organized in groups automatically by using the custering 
algorithm. From this class can be requested the number of groups and the groups list in an iterator.

As it was mentioned earlier the \emph{ToServer} class accesses the web service. This class has three main strings with the 
parameters to access the web server \emph{URL}, \emph{NAMESPACE} and \emph{SOAP\_ACTION}. The URL IP address can be set by 
using the \emph{setIP} method. The user must know, before creating the account, if the nick name is available. So as to check 
this the \emph{exists} function can be used, which takes as a parameter the nick and returns whether it exists or not. 
When a user makes a new registration the method \emph{newUser} is called, which takes as parameters the new user's information 
and password and finally tries to create that account in the web service. It returns whether it succeeded or not.

The \emph{login} method takes as parameters the nick and password of a user and tries to log into the web service, if everything 
is correct an authentication object will be sent for the user in order to identify him, as a session in the Web service. 
If it does not succeed null is returned. Also the authentication object can be checked using the \emph{logged} function. 
There is also a \emph{logout} function that allows the user to log out, deleting the authentication object. The \emph{getAuth} 
private function returns the current authentication object if it did not expired, based on the \emph{sessionLive}. If it expires, 
the method will try to login again and get a new \emph{Auth} object. The \emph{setAuth} method is used so as to this this 
private authentication object

Ones the user has correctly  logged in,  he can request his own information, by calling the \emph{myUser} function. This will 
return a \emph{KUserInfo} containing the user's account information. If the user wants to change his account information he 
should call the \emph{changeUser} with the new user account information and password. This returns true if succeeds, 
otherwise false. When a logged in user searches for other users in order to add them as friends, a new user list is returned 
from function \emph{searchFriend}. This function takes as parameters the fields nick, name, surname and country.

When a new note will be registered, the \emph{setNote} method is called which takes a parameter the two float numbers that 
correspond to the location, latitude and longitude, the note's text and the photo. The photo parameter can be null. This 
function returns true if the note was registered otherwise false. To make a friendship request, or to accept a user's request 
the \emph{askFriend} function must be called with the user's identification number. So as to take the friend requests for a user 
the \emph{getRequests} function is called, and it is returned the list of \emph{KUserInfo} objects that correspond to the users.
Furthermore,in order to get the friend's user information the function \emph{getFriends} should be used, which returns the list of 
\emph{UserInfo} objects containing friends.

The application can send the user's position by using the \emph{logPosition} method. This method sends the location passed as 
arguments to the web service and the authenticated user's position will be updated and registered in his history. If is it needed 
to get a user's history of positions, the \emph{getPositions} method is called with the user's identification number and the 
number of positions we want as parameters. If the authenticated user has rights we will get the list of \emph{KPosition} objects.
To get the last notes of a user the \emph{getNotes} function has to be called with the owner's identification number, the number 
of notes wanted and whether the user wants the photos to be attached. Furthermore, if the user wants to get a note by 
identification number the \emph{getNote} function is called which returns the note with the photo.

Finally, there is a private function that returns the \emph{SHA1} hash code of a given string to be send as password to the web 
service, because the server asks this encoding for security reasons. There are also several fields necessary to implement 
the cache system, all of them are private in the class.

